package com.moyq5.permit.center.web.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.moyq5.mvc.framework.general.Body;
import com.moyq5.mvc.framework.general.Sorting;
import com.moyq5.mvc.framework.jackson.result.ListResult;
import com.moyq5.mvc.framework.jackson.result.ObjectResult;
import com.moyq5.mvc.framework.jackson.result.StatusResult.Status;
import com.moyq5.mvc.framework.utils.StringUtils;
import com.moyq5.permit.center.bean.entity.Agent;
import com.moyq5.permit.center.bean.entity.Merch;
import com.moyq5.permit.center.bean.entity.Shop;
import com.moyq5.permit.center.bean.entity.SysAdmin;
import com.moyq5.permit.center.bean.entity.SysAdminRole;
import com.moyq5.permit.center.bean.entity.SysPermit;
import com.moyq5.permit.center.bean.entity.SysRole;
import com.moyq5.permit.center.bean.enums.SysPlatform;
import com.moyq5.permit.center.bean.search.SysAdminRole4Search;
import com.moyq5.permit.center.bean.search.SysPermit4Search;
import com.moyq5.permit.center.feign.client.SysAdminRoleFeign;
import com.moyq5.permit.center.feign.client.SysPermitFeign;
import com.moyq5.permit.center.web.jackson.filter.AgentFilter;
import com.moyq5.permit.center.web.jackson.filter.MerchFilter;
import com.moyq5.permit.center.web.jackson.filter.ShopFilter;
import com.moyq5.permit.center.web.jackson.filter.SysPermitFilter;

/**
 * 
 * @author Moyq5
 * @date 2019年7月12日
 */
@RestController
@RequestMapping("/login")
public class LoginController extends BaseController {
	
	@Autowired
	private SysAdminRoleFeign adminRoleFeign;
	@Autowired
	private SysPermitFeign permitFeign;
	
	/**
	 * 获取账号用户名，组织名
	 * @author Moyq5
	 * @date 2019年8月14日
	 * @return
	 * @throws JsonProcessingException
	 */
	@GetMapping(value="/name", produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
	@ResponseBody
	public String name() throws JsonProcessingException {
		ObjectResult<String> result = new ObjectResult<String>();
		String name = null;
		if (null != getLoginAgentId()) {
			name = getLoginAgent().getName();
		} else if (null != getLoginMerchId()) {
			name = getLoginMerch().getName();
		} else if (null != getLoginShopId()) {
			name = getLoginShop().getName();
		}
		if (null != name) {
			name += "，" + getLoginAdmin().getName();
		} else {
			name = getLoginAdmin().getName();
		}
		result.setData(name);
		result.setOk();
		return toJson(result, AgentFilter.get());
	}
	
	/**
	 * 获取当前账号的代理商信息
	 * @author Moyq5
	 * @date 2019年8月14日
	 * @return
	 * @throws JsonProcessingException
	 */
	@GetMapping(value="/agent", produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
	@ResponseBody
	public String agent() throws JsonProcessingException {
		ObjectResult<Agent> result = new ObjectResult<Agent>();
		result.setData(getLoginAgent());
		result.setOk();
		return toJson(result, AgentFilter.get());
	}
	
	/**
	 * 获取当前账号的商户信息
	 * @author Moyq5
	 * @date 2019年8月14日
	 * @return
	 * @throws JsonProcessingException
	 */
	@GetMapping(value="/merch", produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
	@ResponseBody
	public String merch() throws JsonProcessingException {
		ObjectResult<Merch> result = new ObjectResult<Merch>();
		result.setData(getLoginMerch());
		result.setOk();
		return toJson(result, MerchFilter.get());
	}
	
	/**
	 * 获取当前账号的门店信息
	 * @author Moyq5
	 * @date 2019年8月14日
	 * @return
	 * @throws JsonProcessingException
	 */
	@GetMapping(value="/shop", produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
	@ResponseBody
	public String shop() throws JsonProcessingException {
		ObjectResult<Shop> result = new ObjectResult<Shop>();
		result.setData(getLoginShop());
		result.setOk();
		return toJson(result, ShopFilter.get());
	}
	
	/**
	 * 获取账号菜单列表
	 * @author Moyq5
	 * @date 2019年8月2日
	 * @return
	 * @throws JsonProcessingException
	 */
	@GetMapping(value="/menus", produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
	@ResponseBody
	public String menus() throws JsonProcessingException {
		Long adminId = getLoginAdminId();
		
		// 获取角色列表
		SysAdmin admin = new SysAdmin();
		admin.setAdminId(adminId);
		SysRole role = new SysRole();
		//role.setPlatform(SysPlatform.AUTH);
		SysAdminRole4Search adminRole4s = new SysAdminRole4Search();
		adminRole4s.setAdmin(admin);
		adminRole4s.setRole(role);
		ListResult<SysAdminRole> adminRoleListRst = adminRoleFeign.listWithRole(new Body<SysAdminRole4Search, Sorting>(adminRole4s, new Sorting()));
		
		ListResult<SysPermit> permitListRst = new ListResult<SysPermit>();
		if (adminRoleListRst.getStatus() != Status.OK 
				|| null == adminRoleListRst.getData()
				|| 0 == adminRoleListRst.getData().size()) {
			permitListRst.setFailMessage("菜单加载有误");
			return toJson(permitListRst, SysPermitFilter.get());
		}
		
		// 获取权限Ids
		StringBuffer sb = new StringBuffer();
		adminRoleListRst.getData().forEach(adminRole -> {
			SysRole role_ = adminRole.getRole();
			if (!StringUtils.isEmpty(role_.getPermits())) {
				sb.append("," + role_.getPermits());
			}
		});
		String[] strIds = sb.toString().split("\\,");
		List<Long> ids = new ArrayList<Long>();
		for (String strId: strIds) {
			try {
				ids.add(Long.parseLong(strId));
			} catch (NumberFormatException e) {
			}
		}
		
		// 获取权限列表
		SysPermit4Search permit4s = new SysPermit4Search();
		permit4s.setPlatform(SysPlatform.AUTH);
		permit4s.setIsMenu(true);
		permit4s.setPermitIdIn(ids.toArray(new Long[ids.size()]));
		Sorting si = new Sorting();
		permitListRst = permitFeign.list(new Body<SysPermit4Search, Sorting>(permit4s, si));
		
		return toJson(permitListRst, SysPermitFilter.get());
	}
}
